<?php
/**
 * Created by PhpStorm
 * User: shen.zk
 * Date: 2023/9/5
 * Time: 10:57
 * Comment: ''
 */

namespace App\Http\Middleware;

use App\Common\Constant;
use App\Repository\MerchantsInfoRepo;
use App\Repository\OpenPlatformRepo;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Log;
use Closure;

class CheckMerchantInfoMiddleware
{
    public function handle(Request $request, Closure $next)
    {
        $secretData = $request->input();
        Log::info("[中间件]验证商户信息中间件接收参数" . json_encode($secretData,JSON_UNESCAPED_UNICODE));

        $err = ['error_code'=>2,"error_msg" => "商户验证中间件-请求参数不能为空",'data'=>'','sign'=>'','encryptKey'=>'','timestamp'=>time()];
        if(empty($secretData))
            return response()->json($err);

        // 校验接入模式
        if (!isset($secretData['access_mode']))
        {
            $err['error_msg'] = '未定义接入模式';
            return response()->json($err);
        }
        // 接入模式参数校验 platform 平台接入  creator创作者接入
        if (!in_array($secretData['access_mode'],['platform','creator']))
        {
            $err['error_msg'] = '接入模式参数错误';
            return response()->json($err);
        }

        // 商户号校验
        if (!isset($secretData['access_mid']) || empty($secretData['access_mid']))
        {
            $err['error_msg'] = '接入商户号不能为空';
            return response()->json($err);
        }
        // 请求时间校验
        if (!isset($secretData['timestamp']) || empty($secretData['timestamp']))
        {
            $err['error_msg'] = '请求时间不能为空';
            return response()->json($err);
        }
        // 请求时间格式校验
        if (!is_integer($secretData['timestamp']))
        {
            $err['error_msg'] = '请求时间格式不正确';
            return response()->json($err);
        }

        // 获取商户号是否存在
        $cacheInfo = Cache::get(Constant::MERCHANTID.':'.$secretData['access_mid']);

        if (empty($cacheInfo))
        {
            if ($secretData['access_mode'] == 'platform')
            {
                $merchantsInfo = OpenPlatformRepo::getInstance()->getByPlatformNo($secretData['access_mid']);
                if (empty($merchantsInfo))
                {
                    $err['error_msg'] = '平台商户不存在';
                    return response()->json($err);
                }

                if (empty($merchantsInfo->public_key))
                {
                    $err['error_msg'] = '平台商户未配置必要信息';
                    return response()->json($err);
                }
            }

            if ($secretData['access_mode'] == 'creator')
            {
                // 获取创作者商户信息
                $merchantsInfo = MerchantsInfoRepo::getInstance()->getByMerchantIdRepo($secretData['access_mid']);
                if (empty($merchantsInfo))
                {
                    $err['error_msg'] = '创作者商户不存在';
                    return response()->json($err);
                }


                if (empty($merchantsInfo->public_key))
                {
                    $err['error_msg'] = '创作者商户未配置必要信息';
                    return response()->json($err);
                }
            }

            // 反向写入cache
            Cache::put(Constant::MERCHANTID.':'.$secretData['access_mid'],json_encode($merchantsInfo));
        }
        return $next($request);
    }
}
